<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 下午5:44
 */

class ModComplex {
	
	public static function channelSumData($params){
		$where  = ' WHERE RecDate BETWEEN :begin AND :end
        		       AND Period=:period
				       AND SID=:SID
        		       AND GameSrc=:game ';
		
		$args   = array(
				':begin'    => $params['time_start'],
				':end'      => $params['time_end'],
				':period'   => 'Daily',
				':game'     => $params['g'],
				':SID'      => 'ALL',
				
		);
		
// 		if($params['special_id'] != 'ALL'){
// 			//查询该专服下的渠道tab
// 			$modChannel = new ModChannel();
// 			$channelList = $modChannel->getChannelForSpecial($params['special_id']);
// 			$tabs = '';
// 			foreach ($channelList as $key=>$val){
// 				$tabs .= ','.$val['tab'];
// 			}
// 			$tabs =  substr($tabs, 1);
			
// 			if($tabs != ''){
// 				$where .= " AND Platform in ({$tabs}) ";
// 			}
// 		}
		
		$table = 'tblGame';
		$field =  'Platform,SUM(RegUser) as RegUser,SUM(CumRegUser) as CumRegUser,SUM(RoleUser) as RoleUser,SUM(CumRoleUser) as CumRoleUser,';
		$field .= 'SUM(LoginUser) as LoginUser,SUM(OldUser) as OldUser,SUM(LoginCount) as LoginCount,SUM(MaxOnline) as MaxOnline,SUM(PayUser) as PayUser,SUM(PayCount) as PayCount,';
		$field .= 'SUM(PayMoney) as PayMoney,SUM(PayGold) as PayGold,SUM(UsedGold) as UsedGold,SUM(FirstPayUser) as FirstPayUser,SUM(FirstPayCount) as FirstPayCount,SUM(FirstPayMoney) as FirstPayMoney,';
		$field .= 'SUM(FirstPayGold) as FirstPayGold,SUM(Remain1Day) as Remain1Day,SUM(Remain2Day) as Remain2Day,SUM(Remain3Day) as Remain3Day,SUM(Remain7Day) as Remain7Day,SUM(Remain14Day) as Remain14Day,';
		$field .= 'SUM(Remain30Day) as Remain30Day,SUM(Remain60Day) as Remain60Day,SUM(Remain90Day) as Remain90Day,SUM(AvgOnlineTime) as AvgOnlineTime,SUM(AvgPeakUser) as AvgPeakUser,SUM(LogoutUser) as LogoutUser,';
		$field .= 'SUM(PresentGold) as PresentGold,SUM(DAU) as DAU,SUM(Remain4Day) as Remain4Day,SUM(Remain5Day) as Remain5Day,SUM(Remain6Day) as Remain6Day';
		$sql = "SELECT {$field} FROM {$table} {$where} GROUP BY Platform";
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
		return $dbLink->query($sql, $args);
	}

    public static function indexData($params){
        $where  = ' WHERE RecDate BETWEEN :begin AND :end 
        		       AND Period=:period
        		       AND GameSrc=:game ';
        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => $params['period'],
            ':game'     => $params['g'],
        );
        
        $user = AccAuth::me();
        if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
        	$where .= " AND Platform=:p ";
        	$args[':p'] = $params['os'];
        }
        
        if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
        	$modServer = new ModServer();
        	$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
        	$where .= " AND ( ";
        	foreach ($list as $key=>$val){
        		$where .= " SID = '{$val['develop_id']}'  or";
        	}
        	$where = substr($where, 0, -2);
        	$where .= " ) ";
        }else{
        	$where .= " AND SID=:ser ";
        	$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
        }

        $table = 'tblGame';

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }

} 
